package com.ithui.tidal.common.thread;

import lombok.AllArgsConstructor;

import java.util.concurrent.ThreadFactory;

/**
 * @Author: ithui
 * @CreateTime: 2025-07-31
 * @Description: 自定义线程工厂，添加自定义处理异常的线程工厂
 */
@AllArgsConstructor
public class MyThreadFactory implements ThreadFactory {

    private static final MyUncaughtExceptionHandler MY_UNCAUGHT_EXCEPTION_HANDLER = new MyUncaughtExceptionHandler();
    private ThreadFactory original;


    @Override
    public Thread newThread(Runnable r) {
        Thread thread = original.newThread(r);// 执行spring线程自己的创建逻辑
        // 额外装饰我们需要的创建逻辑
        thread.setUncaughtExceptionHandler(MY_UNCAUGHT_EXCEPTION_HANDLER);
        return thread;
    }
}
